Simplify GetInterfaces handling
authorMatthias Clasen <mclasen@redhat.com>
Sun, 11 Oct 2020 15:06:29 +0000 (11:06 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 12 Oct 2020 19:10:40 +0000 (15:10 -0400)
It is error prone to keep the same conditions in sync
in two places. Instead, just assemble the list of interfaces
as we register objects, and use when GetInterfaces is called.

gtk/a11y/gtkatspicontext.c

index e67485960fa1ffa96c06876702c3e3d2087a558d..ba21a2d509dc30d9e24e18211e6baa5156616e61 100644 (file)
@@ -79,6 +79,12 @@ struct _GtkAtSpiContext
    * associated to the GtkATContext
    */
   GDBusConnection *connection;
+
+  /* Accerciser refuses to work unless we implement a GetInterface
+   * call that returns a list of all implemented interfaces. We
+   * collect the answer here.
+   */
+  GVariant *interfaces;
 };
 
 enum
@@ -429,24 +435,7 @@ handle_accessible_method (GDBusConnection       *connection,
     }
   else if (g_strcmp0 (method_name, "GetInterfaces") == 0)
     {
-      GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
-      GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("as"));
-
-      g_variant_builder_add (&builder, "s", "org.a11y.atspi.Accessible");
-      if (GTK_IS_LABEL (accessible) ||
-          GTK_IS_TEXT (accessible) ||
-          GTK_IS_TEXT_VIEW (accessible))
-        g_variant_builder_add (&builder, "s", "org.a11y.atspi.Text");
-
-      if (GTK_IS_LEVEL_BAR (accessible) ||
-          GTK_IS_PANED (accessible) ||
-          GTK_IS_PROGRESS_BAR (accessible) ||
-          GTK_IS_RANGE (accessible) ||
-          GTK_IS_SCALE_BUTTON (accessible) ||
-          GTK_IS_SPIN_BUTTON (accessible))
-        g_variant_builder_add (&builder, "s", "org.a11y.atspi.Value");
-
-      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(as)", &builder));
+      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(@as)", self->interfaces));
     }
 
 }
@@ -1279,7 +1268,9 @@ static void
 gtk_at_spi_context_register_object (GtkAtSpiContext *self)
 {
   GtkWidget *widget = GTK_WIDGET (gtk_at_context_get_accessible (GTK_AT_CONTEXT (self)));
+  GVariantBuilder interfaces = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_STRING_ARRAY);
 
+  g_variant_builder_add (&interfaces, "s", "org.a11y.atspi.Accessible");
   g_dbus_connection_register_object (self->connection,
                                      self->context_path,
                                      (GDBusInterfaceInfo *) &atspi_accessible_interface,
@@ -1292,6 +1283,7 @@ gtk_at_spi_context_register_object (GtkAtSpiContext *self)
       GTK_IS_TEXT (widget) ||
       GTK_IS_TEXT_VIEW (widget))
     {
+      g_variant_builder_add (&interfaces, "s", "org.a11y.atspi.Text");
       g_dbus_connection_register_object (self->connection,
                                          self->context_path,
                                          (GDBusInterfaceInfo *) &atspi_text_interface,
@@ -1308,6 +1300,7 @@ gtk_at_spi_context_register_object (GtkAtSpiContext *self)
       GTK_IS_SCALE_BUTTON (widget) ||
       GTK_IS_SPIN_BUTTON (widget))
     {
+      g_variant_builder_add (&interfaces, "s", "org.a11y.atspi.Value");
       g_dbus_connection_register_object (self->connection,
                                          self->context_path,
                                          (GDBusInterfaceInfo *) &atspi_value_interface,
@@ -1316,6 +1309,8 @@ gtk_at_spi_context_register_object (GtkAtSpiContext *self)
                                          NULL,
                                          NULL);
     }
+
+  self->interfaces = g_variant_ref_sink (g_variant_builder_end (&interfaces));
 }
 
 static void
@@ -1336,6 +1331,7 @@ gtk_at_spi_context_finalize (GObject *gobject)
 
   g_free (self->bus_address);
   g_free (self->context_path);
+  g_clear_pointer (&self->interfaces, g_variant_unref);
 
   G_OBJECT_CLASS (gtk_at_spi_context_parent_class)->finalize (gobject);
 }